home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / CBGRX103.ZIP / contrib / libgrx / src / loadfont.c < prev    next >
Text File  |  1993-12-06  |  4KB  |  140 lines

  1. /**
  2.  ** LOADFONT.C
  3.  **
  4.  **  Copyright (C) 1992, Csaba Biegl
  5.  **    820 Stirrup Dr, Nashville, TN, 37221
  6.  **    csaba@vuse.vanderbilt.edu
  7.  **
  8.  **  This file is distributed under the terms listed in the document
  9.  **  "copying.cb", available from the author at the address above.
  10.  **  A copy of "copying.cb" should accompany this file; if not, a copy
  11.  **  should be available from where this file was obtained.  This file
  12.  **  may not be distributed without a verbatim copy of "copying.cb".
  13.  **  You should also have received a copy of the GNU General Public
  14.  **  License along with this program (it is in the file "copying");
  15.  **  if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16.  **  Cambridge, MA 02139, USA.
  17.  **
  18.  **  This program is distributed in the hope that it will be useful,
  19.  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  **  GNU General Public License for more details.
  22.  **/
  23.  
  24. #include "grx.h"
  25. #include "libgrx.h"
  26. #include "grxfile.h"
  27. #include "grxfont.h"
  28. #include "gmalloc.h"
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <fcntl.h>
  34. #include <io.h>
  35.  
  36.  
  37. char   _GrFontPath[80] = { "" };
  38. static ResList *loaded = NULL;
  39.  
  40. void GrSetFontPath(char *path)
  41. {
  42.     _GrSetPath(((path == NULL) ? getenv(FNTENV) : path),_GrFontPath);
  43. }
  44.  
  45. GrFont *GrLoadFont(char *name)
  46. {
  47.     FntFileHdr hdr;
  48.     GrFont *font;
  49.     char fullname[120];
  50.     int  file;
  51.  
  52.     _GrGetFname(name,_GrFontPath,FNTENV,FNTEXT,fullname);
  53.     if(fullname[0] == '@') {
  54.         font = GrLoadBIOSFont(fullname);
  55.         if(font != NULL) return(font);
  56.         _GrGetFname(&name[2],_GrFontPath,FNTENV,FNTEXT,fullname);
  57.     }
  58.     font = (GrFont *)_GrLookupInResList(fullname,loaded);
  59.     if(font != NULL) return(font);
  60.     if(((file = _GrFileOpen(fullname)) == EOF) ||
  61.         (read(file,&hdr,sizeof(FntFileHdr)) != sizeof(FntFileHdr)) ||
  62.         (hdr.magic != FONT_MAGIC)) {
  63.         if(file != EOF) _GrFileClose(file);
  64.         return(NULL);
  65.     }
  66.     if(hdr.h.fnt_isfixed) {
  67.         FixedFont *result;
  68.         char far  *bits;
  69.  
  70. #ifdef _MAXMEMPLANESIZE
  71.         if(hdr.bitmapsize >= _MAXMEMPLANESIZE) {
  72.         _GrFileClose(file);
  73.         return(NULL);
  74.         }
  75. #endif
  76.         result = _GrMalloc(sizeof(FixedFont));
  77.         bits = _GrFarMalloc(hdr.bitmapsize + 1);
  78.         if((result == NULL) ||
  79.            (bits == (char far *)NULL) ||
  80.            (_GrFarRead(file,bits,hdr.bitmapsize) != hdr.bitmapsize)) {
  81.         if(result != NULL) _GrFree(result);
  82.         if(bits != (char far *)NULL) _GrFarFree(bits);
  83.         _GrFileClose(file);
  84.         return(NULL);
  85.         }
  86.         result->h = hdr.h;
  87.         result->ff_bits = bits;
  88.         result->ff_chrsize =
  89.         ((hdr.h.fnt_width + 7) >> 3) * hdr.h.fnt_height;
  90.         _GrFileClose(file);
  91.         font = (GrFont *)result;
  92.     }
  93.     else {
  94.         int ii,numc = hdr.h.fnt_maxchar - hdr.h.fnt_minchar + 1;
  95.         int tsize1 = numc * sizeof(short);
  96.         int tsize2 = (numc - 1) * sizeof(char far *);
  97.         PropFont *result = _GrMalloc(sizeof(PropFont) + tsize1 + tsize2);
  98.         char far *bits   = _GrFarMalloc(hdr.bitmapsize + 1);
  99.  
  100.         if((result == NULL) ||
  101.            (bits == (char far *)NULL) ||
  102.            (read(file,&result->pf_bits[numc],tsize1) != tsize1) ||
  103.            (_GrFarRead(file,bits,hdr.bitmapsize) != hdr.bitmapsize)) {
  104.         if(result != NULL) _GrFree(result);
  105.         if(bits != (char far *)NULL) _GrFarFree(bits);
  106.         _GrFileClose(file);
  107.         return(NULL);
  108.         }
  109.         result->h = hdr.h;
  110.         result->pf_width = (short *)(&result->pf_bits[numc]);
  111.         result->pf_maxwidth = 0;
  112.         result->pf_minwidth = 32767;
  113.         for(ii = 0; ii < numc; ii++) {
  114.         tsize1 = result->pf_width[ii];
  115.         tsize2 = ((tsize1 + 7) >> 3) * hdr.h.fnt_height;
  116.         result->pf_bits[ii] = bits;
  117.         bits = (char far *)((char huge *)bits + (long)tsize2);
  118.         if(result->pf_minwidth > tsize1) result->pf_minwidth = tsize1;
  119.         if(result->pf_maxwidth < tsize1) result->pf_maxwidth = tsize1;
  120.         }
  121.         _GrFileClose(file);
  122.         font = (GrFont *)result;
  123.     }
  124.     _GrAddToResList((void *)font,fullname,&loaded);
  125.     return(font);
  126. }
  127.  
  128. void GrUnloadFont(GrFont *font)
  129. {
  130.     if((font != NULL) && !font->fnt_internal) {
  131.         _GrRemoveFromResList(font,&loaded);
  132.         _GrFarFree(font->fnt_isfixed ?
  133.         ((FixedFont *)font)->ff_bits :
  134.         ((PropFont *)font)->pf_bits[0]
  135.         );
  136.         _GrFree(font);
  137.     }
  138. }
  139.  
  140.